from osv import fields
from osv import osv
import wizard
import netsvc
import pooler
import time
from osv.orm import browse_record

#Quotation to Project(s)
class wiz_quotation_projects_open(wizard.interface):
    def _open_projects_from_quotation(self, cr, uid, data, context):
        quo_ids=','.join(map(str,data['ids']))
        cr.execute("(Select\
                        project_e_id as id\
                    from \
                        sale_order\
                    where project_e_id is not null and id in (%s))\
                    Union\
                    (Select\
                        project_m_id as id\
                    from \
                        sale_order\
                    where project_m_id is not null and id in (%s))" % (quo_ids,quo_ids))
        prj_ids=[]
        for prj_id in cr.fetchall():
            prj_ids.append(prj_id)
        prj_ids= ','.join(map(str,prj_ids))
        
        value = {
            'domain': "[('id', 'in',["+prj_ids+"])]",
            'name': 'Job',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.project',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_projects_from_quotation, 'state':'end'}
        }
    }
wiz_quotation_projects_open('quotation.projects.open')

#Quotation to contract
class wiz_quotation_contract_open(wizard.interface):
    def _open_contract_from_quotation(self, cr, uid, data, context):
        quo_ids=','.join(map(str,data['ids']))
        cr.execute("Select\
                        contract_id as id\
                    from \
                        sale_order\
                    where id in (%s)" % (quo_ids))
        ctc_ids=[]
        for ctc_id in cr.fetchall():
            ctc_ids.append(ctc_id)
        ctc_ids= ','.join(map(str,ctc_ids))
        value = {
            'domain': "[('id', 'in',["+ctc_ids+"])]",
            'name': 'Contract',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.contract.client',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_contract_from_quotation, 'state':'end'}
        }
    }
wiz_quotation_contract_open('quotation.contract.open')

#Quotation to payment
class wiz_quotation_payment_open(wizard.interface):
    def _open_payment_from_quotation(self, cr, uid, data, context):
        quo_ids=','.join(map(str,data['ids']))
        cr.execute("Select id from kdvn_payment_from_client where contract_id in (Select\
                        contract_id as id\
                    from \
                        sale_order\
                    where id in (%s))" % (quo_ids))
        ctc_ids=[]
        for ctc_id in cr.fetchall():
            ctc_ids.append(ctc_id)
        ctc_ids= ','.join(map(str,ctc_ids))
        value = {
            'domain': "[('id', 'in',["+ctc_ids+"])]",
            'name': 'Payment from Client',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.payment.from.client',
            'view_id': False,
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_payment_from_quotation, 'state':'end'}
        }
    }
wiz_quotation_payment_open('quotation.pfc.open')